﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using KandaAlpha.Infrastructure.Repository.db4o;
using KandaAlpha.Domain.Repository;
using KandaAlpha.Domain.Model.Entities; 

namespace KandaAlpha.Infrastructure.Repository.db4o.Tests
{
    /// <summary>
    /// Summary description for 
    /// </summary>
    [TestClass]
    public class ClassifierRepositoryTest : TestBase 
    {
        private readonly IClassifierRepository _classifierRepository;

        public ClassifierRepositoryTest()
        {
            _classifierRepository = new ClassifierRepository(ObjectContainerManager); 
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }


        [ClassCleanup()]
        public static void MyClassCleanup()
        {
            // close the container
            ObjectContainerManager.ObjectContainer.Close();
            // close the server
            ObjectContainerManager.CloseServer();
        }

        [TestInitialize()]
        public void MyTestInitialize()
        {
            var classifier = new Classifier
            {
                ID = 1,
                ClassifierRef = "Classifier1",
                ClassifierName = "Classifier 1",
            };
            _classifierRepository.Add(classifier);
            ObjectContainerManager.ObjectContainer.Commit();
        }

        [TestCleanup()]
        public void MyTestCleanup()
        {
            var classifiers = _classifierRepository.GetAll();
            foreach (var classifier in classifiers)
            {
                _classifierRepository.Delete(classifier);
            }
            ObjectContainerManager.ObjectContainer.Commit();
        }

        [TestMethod]
        public void Classifier_Update_Test()
        {
            var classifier = _classifierRepository.Load(c => c.ID == 1);
            Assert.IsNotNull(classifier);
            classifier.ClassifierName = "Classifier 1 Updated";
            ObjectContainerManager.ObjectContainer.Commit();
            classifier = _classifierRepository.Load(c => c.ID == 1);
            Assert.AreEqual("Classifier 1 Updated", classifier.ClassifierName);
        }

        //[TestMethod]
        //public void Classifier_Update_Test()
        //{
        //    var classifier = _classifierRepository.Load(c => c.ID == 1);
        //    Assert.IsNotNull(classifier);
        //    classifier.ClassifierName = "Classifier 1 Updated";
        //    ObjectContainerManager.ObjectContainer.Commit();
        //    classifier = _classifierRepository.Load(c => c.ID == 1);
        //    Assert.AreEqual("Classifier 1 Updated", classifier.ClassifierName);
        //}


        [TestMethod]
        public void Classifier_Delete_Test()
        {
            var classifier = _classifierRepository.Load(c => c.ID == 1);
            Assert.IsNotNull(classifier);
            _classifierRepository.Delete(classifier);
            ObjectContainerManager.ObjectContainer.Commit();
            classifier = _classifierRepository.Load(c => c.ID == 1);
            Assert.IsNull(classifier);
        }

        [TestMethod]
        public void Classifier_Add_Test()
        {

            var classifier = new Classifier
            {
                ID = 100,
                ClassifierRef = "Classifier100",
                ClassifierName = "Classifier 100",
             
            };

            var children = new List<Classifier> 
               {
                   new Classifier 
                   {
                       ID = 101, 
                       Parent = classifier,
                       ClassifierRef  = "Classifier101",
                       ClassifierName = "Classifier 101"
                   },
                   new Classifier 
                   {
                       ID = 102, 
                       Parent = classifier,
                       ClassifierRef  = "Classifier102",
                       ClassifierName = "Classifier 102"
                   }
               };


            _classifierRepository.Add(classifier);

            // commit changes
           ObjectContainerManager.ObjectContainer.Commit();

           var actual = _classifierRepository.Load(c => c.ID == 100);

            Assert.IsNotNull(actual); 

            // check children count
            Assert.AreEqual(2, actual.Children.Count);

            // check children's parents
            foreach (var child in actual.Children)
            {
                Assert.IsNotNull(child.Parent.ID = actual.ID); 
            }

        }
    }
}
